package com.ljc.service;

import com.ljc.dao.*;
import com.ljc.entity.*;
import com.ljc.vo.AssignExpert;
import com.ljc.vo.ProjectPlanningReviewSearch;
import com.ljc.vo.ProjectPlanningSearch;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProjectPlanningServiceImpl implements ProjectPlanningService {
    @Autowired
    private ProjectPlanningDao projectPlanningDao;

    @Autowired
    private ExperimentSituationDao experimentSituationDao;

    @Autowired
    private DevicePurchasePlanningDao devicePurchasePlanningDao;

    @Autowired
    private ProjectPlanningReviewDao projectPlanningReviewDao;

    @Autowired
    private ProjectPlanningArgumentDao projectPlanningArgumentDao;

    @Autowired
    private DevicePurchasePlanningArgumentDao devicePurchasePlanningArgumentDao;

    @Autowired
    private AttachmentDao attachmentDao;

    @Autowired
    private DeviceArgumentGatherDao deviceArgumentGatherDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProjectPlanning(ProjectPlanning projectPlanning) {
        projectPlanning.setCreatedDate(new Date());
        projectPlanningDao.addPurchasePlanning(projectPlanning);
        List<ExperimentSituation> experimentSituations = projectPlanning.getExperimentSituations();
        if (experimentSituations != null && experimentSituations.size() > 0) {
            for (ExperimentSituation situation : experimentSituations) {
                situation.setProjectPlanningId(projectPlanning.getId());
            }
            experimentSituationDao.addExperimentSituations(experimentSituations);
        }
        List<DevicePurchasePlanning> devicePurchasePlannings = projectPlanning.getDevicePurchasePlannings();
        if (devicePurchasePlannings != null && devicePurchasePlannings.size() > 0) {
            for (DevicePurchasePlanning devicePurchasePlanning : devicePurchasePlannings) {
                devicePurchasePlanning.setProjectPlanningId(projectPlanning.getId());
            }
            if (devicePurchasePlannings.size() > 0)
                devicePurchasePlanningDao.add(devicePurchasePlannings);
        }
        List<Attachment> attachments = projectPlanning.getAttachments();
        if (attachments != null && attachments.size() > 0) {
            for (Attachment attachment : attachments) {
                attachment.setMainId(projectPlanning.getId());
                attachment.setType(1);
            }
            attachmentDao.add(attachments);
        }

    }

    @Override
    public void deleteById(int id) {
        projectPlanningDao.deleteById(id);
    }

    @Override
    public List<ProjectPlanning> getByUserId(int id) {
        return projectPlanningDao.getByUserId(id);
    }

    @Override
    public List<ProjectPlanning> searchByCollegeUser(ProjectPlanningSearch search) {
        List<ProjectPlanning> projectPlannings = projectPlanningDao.searchByCollegeUser(search);
        return projectPlannings;
    }

    @Override
    public ProjectPlanning getById(int id) {

        ProjectPlanning projectPlanning = projectPlanningDao.getById(id);
        List<DevicePurchasePlanning> devicePurchasePlannings = devicePurchasePlanningDao.findByProjectPlanningId(id);
        projectPlanning.setDevicePurchasePlannings(devicePurchasePlannings);
        List<Attachment> attachments = attachmentDao.getByTypeAndMainId(1, id);
        projectPlanning.setAttachments(attachments);
        return projectPlanning;

    }

    @Override
    public List<ProjectPlanning> searchByAdministrator(ProjectPlanningReviewSearch search) {
        return projectPlanningDao.searchByAdministrate(search);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void review(ProjectPlanningReview review) {
        if (review.getResult() == 1) {
            projectPlanningDao.setStatus(review.getProjectPlanningId(), 4);
        } else {
            projectPlanningDao.setStatus(review.getProjectPlanningId(), 3);
        }
        projectPlanningReviewDao.add(review);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void setStatus(int id, int status) {
        projectPlanningDao.setStatus(id, status);
        if (status==5 || status==6){
            List<DevicePurchasePlanning> devicePurchasePlannings = devicePurchasePlanningDao.findByProjectPlanningId(id);
            if (devicePurchasePlannings!=null && devicePurchasePlannings.size()>0){
                for (DevicePurchasePlanning devicePurchasePlanning : devicePurchasePlannings) {
                    deviceArgumentGatherDao.add(devicePurchasePlanning.getId());
                }
            }
        }
    }


    //建立项目-专家关联
    //建立项目设备购置论证-专家关联
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void assignExpert(List<AssignExpert> assignExperts) {
        projectPlanningDao.setStatus(assignExperts.get(0).getProjectPlanningId(), 7);
        projectPlanningArgumentDao.add(assignExperts);

        List<DevicePurchasePlanning> devicePurchasePlannings = devicePurchasePlanningDao.findByProjectPlanningId(assignExperts.get(0).getProjectPlanningId());
        for (DevicePurchasePlanning devicePurchasePlanning : devicePurchasePlannings) {
            for (AssignExpert assignExpert : assignExperts) {
                DevicePurchasePlanningArgument devicePurchasePlanningArgument = new DevicePurchasePlanningArgument();
                int expertId = assignExpert.getExpertId();
                devicePurchasePlanningArgument
                        .setDevicePurchasePlanningId(devicePurchasePlanning.getId());
                devicePurchasePlanningArgument.setExpertId(expertId);
                devicePurchasePlanningArgumentDao.add(devicePurchasePlanningArgument);
            }

        }
    }

    @Override
    public List<ProjectPlanning> searchByExpertUser(ProjectPlanningSearch search) {
        return projectPlanningDao.searchByExpertUser(search);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateProjectPlanning(ProjectPlanning projectPlanning) {
        projectPlanningDao.updatePurchasePlanning(projectPlanning);
        int id = projectPlanning.getId();

        experimentSituationDao.deleteByProjectPlanningId(id);
        List<ExperimentSituation> experimentSituations = projectPlanning.getExperimentSituations();
        if (experimentSituations!=null && experimentSituations.size()>0){
            for (ExperimentSituation experimentSituation : experimentSituations) {
                experimentSituation.setProjectPlanningId(id);
            }
            experimentSituationDao.addExperimentSituations(experimentSituations);
        }




        devicePurchasePlanningDao.deleteByProjectPlanningId(id);
        List<DevicePurchasePlanning> devicePurchasePlannings = projectPlanning.getDevicePurchasePlannings();
        if (devicePurchasePlannings!=null && devicePurchasePlannings.size()>0){
            for (DevicePurchasePlanning devicePurchasePlanning : devicePurchasePlannings) {
                devicePurchasePlanning.setProjectPlanningId(id);
            }
            devicePurchasePlanningDao.add(devicePurchasePlannings);
        }


        attachmentDao.deleteByProjectPlanningId(id);
        List<Attachment> attachments = projectPlanning.getAttachments();
        if(attachments!=null && attachments.size()>0){
            for (Attachment attachment : attachments) {
                attachment.setType(1);
                attachment.setMainId(id);
            }
            attachmentDao.add(attachments);
        }


    }


}
